import { createApp } from 'vue';
import pinia from '/@/stores/index';
import App from '/@/App.vue';
import router from '/@/router';
import { directive } from '/@/directive/index';
import { i18n } from '/@/i18n/index';
import other from '/@/utils/other';

import ElementPlus from 'element-plus';
import '/@/theme/index.scss';
import VueGridLayout from 'vue-grid-layout';

const app = createApp(App);

directive(app);
other.elSvg(app);

app.use(pinia).use(router).use(ElementPlus).use(i18n).use(VueGridLayout).mount('#app');

// 注册focus指令
app.directive('focus', {
  mounted(el) {
    el.focus()
  },
  updated(el) {
    el.focus()
  },
})

// 注册enter-number指令
app.directive('enter-number', {
  mounted(el, binding) {
    const decimalPlaces = binding.value || 0
    el.addEventListener('input', (e) => {
      let value = e.target.value
      // 只允许数字、小数点和负号
      value = value.replace(/[^\d.-]/g, '')
      // 确保只有一个小数点
      const parts = value.split('.')
      if (parts.length > 2) {
        value = parts[0] + '.' + parts.slice(1).join('')
      }
      // 限制小数位数
      if (decimalPlaces > 0 && parts[1] && parts[1].length > decimalPlaces) {
        value = parts[0] + '.' + parts[1].substring(0, decimalPlaces)
      }
      e.target.value = value
    })
  },
})
